from bs4 import BeautifulSoup


html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

soup = BeautifulSoup(html, "lxml")

# 2.搜索文档树
# 2.1 find_all(name, attrs, recursive, text, **kwargs)
# 2.2 find()find的用法与find_all一样，区别在于find返回 第一个符合匹配结果,find_all则返回 所有匹配结果的列表
# 根据语法规则,会去匹配所有符合规则的内容
"""
name:
    1.标签名字(传入字符串)
    2.传入正则表达式
    3.传入列表
    4.True 递归的获取所有的标签
"""
res = soup.find_all("a")
print(res)

# 传入正则的时候 不死直接传入正则表达式的字符串
# 传入一个正则的对象
import re
res = soup.find_all(name=re.compile("^p"))
print(res)

# 传列表
print(soup.find_all(["a", "b"]))

# keyword 参数
print(soup.find_all(id='link2'))

"""
text 根据标签文本内容进行搜索
"""
# text 参数:可以搜索文档中的字符串内容，与 name 参数的可选值一样, text 参数接受 字符串 , 正则表达式 , 列表
print(soup.find_all(text="Elsie"))
# [u'Elsie']

print(soup.find_all(text=["Tillie", "Elsie", "Lacie"]))
# [u'Elsie', u'Lacie', u'Tillie']

print(soup.find_all(text=re.compile("Dormouse")))
# [u"The Dormouse's story", u"The Dormouse's story"]

"""
attrs 参数　　接收的事一个字典类型　　{属性名：属性值}
"""
print(soup.find_all(attrs={"id": "link1"}))
print(soup.find_all(attrs={"class": "sister"}, limit=2))

"""
**kwargs  key=value class=sister
"""
print(soup.find_all(class_="sister"))
print(soup.find_all(id="link1"))
